<?php
namespace App\Http\Controllers\Admin;

use App\Services\Admin\VillageService;
use Illuminate\Http\Request;

class VillageController extends BaseController
{
    public function __construct(VillageService $service)
    {
        $this->db = $service;
    }

    /**
     * 选项
     */
    public function getOption(Request $request)
    {
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    /**
     * 省市区
     */
    public function areaByPid(Request $request)
    {
        $pid = $request->post('pid', 0);
        $res = $this->db->areaListByPid($pid);
        return $this->toJson(0, '', $res);
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
//        $headers = $this->getAid($request);
//        if ($headers['code'] != 0) {
//            return $this->toJson(-2, $headers['msg']);
//        }
        $pages['start'] = $request->get('pageStart', 0);
        $pages['limit'] = $request->get('pageLimit', 10);
        $res = $this->db->getIndex($pages);
        if ($res['code']!=0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
//        $headers = $this->getAid($request);
//        if ($headers['code'] != 0) {
//            return $this->toJson(-2, $headers['msg']);
//        }
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['area_id'] = $request->post('areaid', 0);
        $pas['street'] = $request->post('street', '');
        $pas['map'] = $request->post('map', '');
        if ($id) {
            $rst = $this->db->modify($id, $pas);
        } else {
            $rst = $this->db->add($pas);
        }
        if ($rst['code']!=0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
//        $headers = $this->getAid($request);
//        if ($headers['code'] != 0) {
//            return $this->toJson(-2, $headers['msg']);
//        }
        $id = $request->post('id', 0);
        $res = $this->db->del($id);
        if ($res['code']!=0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //地址转坐标
    public function addressToMap(Request $request)
    {
        $address = $request->post('address', '');
        $httpUrl = 'https://apis.map.qq.com/ws/geocoder/v1/';
        $httpUrl .= '?address=' . $address;
        $httpUrl .= '&key=JWYBZ-7LK3K-KRNJA-AGOHX-CIZNK-RCFXV';
        $res = $this->setCurl($httpUrl);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        if ($res['status'] != 0) {
            return $this->toJson(-2, $res['message']);
        }
        return $this->toJson(0, '', $res['result']);
    }
}